﻿using System.Runtime.InteropServices;
using System.IO;
using UnityEngine;

public class SSCInternalAdapter
{
//	private static string _methodStartNews = "doNewsblast";
////	private static string _methodCancelShowNewsblast = "";
//	private static string _methodSetGameObjName = "setNBGameObjName";
//	private static string _methodRateUs = "rateUs";
//	private static string _methodDissmissRateUs = "dissmissRateUs";
//	private static string _methodIsRated = "isRated";
//	private static string _methodOpenWithUrl = "openWithUrl";

	private static string _methodDoNewsBlast = "doNewsBlast";
	private static string _methodSetGameObjName = "setGameObjectName";
	private static string _methodRateUs = "rateUs";
	private static string _methodDissmissRateUs = "dissmissRateUs";
	private static string _methodIsRated = "isRate";
	private static string _methodOpenWithUrl = "openUrl";

	private static string _methodRefreshDCIM = "refreshDCIM";
	private static string _methodGetSdPath = "getSdCardPath";
	private static string _methodPopAlertDialog = "popAlertDialog";
	private static string _methodSendEmail = "sendMailByIntent";
	private static string _methodSendEmailAndPic = "sendEmailAndFilePic";
	private static string _methodShowMoreGames = "showMoreGame";

	private static string _methodMakeToast = "makeToast";
	private static string _methodCheckNetworkAvailable = "checkNetworkAvailable";
	private static string _methodIsTablet = "isTablet";
	private static string _methodDensityScale = "densityScale";
	private static string _methodEndSession = "endSession";
	private static string _methodListAssetFiles = "listAssetFiles";
	private static string _methodShowNetworkAlertDlg = "showNetworkAlertDlg";
	private static string _methodShowLoadingView = "showLoadingView";
	private static string _methodCloseLoadingView = "closeLoadingView";

	private static SSCInternalAdapter _instance;
	private string _gameObjName;

	private SSCInternalAdapter ()
	{
	}

	public static SSCInternalAdapter getInstance(){
		if (_instance == null)
			_instance = new SSCInternalAdapter();
		return _instance;
	}
		
	public void setGameObjName(string name){
		AndroidHelper.getInternalAndroidObject().Call(_methodSetGameObjName,name);
	}

	public void doNewsBlast(){
		AndroidHelper.getInternalAndroidObject().Call(_methodDoNewsBlast);

	}

	public void cancelShowNewsBlast(){
		//		AndroidHelper.getObj().Call(_methodCancelShowNewsblast);
	}

	public void rateUs(){
		AndroidHelper.getInternalAndroidObject().Call(_methodRateUs);
	}

	public void dissmissRateUs(){
		AndroidHelper.getInternalAndroidObject().Call(_methodDissmissRateUs);
	}

	public bool isRated(){
		return AndroidHelper.getInternalAndroidObject().Call<bool>(_methodIsRated);
	}

	public void openUrl(string url){
		AndroidHelper.getInternalAndroidObject().Call(_methodOpenWithUrl,url);
	}

	public string getSdcardPath(){
		string path = AndroidHelper.getInternalAndroidObject().Call<string>(_methodGetSdPath);
		return path;
	}

	public string saveImageToAlbum(string dir,byte[] data){
		string fullpath = null;
		try{
			string path = getSdcardPath ();
			if(path != null && data != null){
				string dirPath = path + "/" +dir;
				if(!Directory.Exists(dirPath)){
					Directory.CreateDirectory(dirPath);
				}
				string filename = System.DateTime.Now.ToString ("yyyyMMddHHmmss") + ".png";
			    fullpath = dirPath + "/" + filename;
			
			
				File.WriteAllBytes(fullpath, data);
			
				refreshDCIM(fullpath);
			}
		}catch(IOException e){
			fullpath = null;
		}
		return fullpath;

	}

	public void sendEmail(string subject,string message){
		AndroidHelper.getInternalAndroidObject().Call(_methodSendEmail,subject,message);
	}

	public void sendEmailAndFilePic(string subject,string message ,string imageFilePath){
		AndroidHelper.getInternalAndroidObject().Call(_methodSendEmailAndPic,subject,message,imageFilePath);
	}


	public void showMoreGames(){
		AndroidHelper.getInternalAndroidObject().Call(_methodShowMoreGames);
	}

	public void refreshDCIM(string filePath){
		AndroidHelper.getInternalAndroidObject().Call(_methodRefreshDCIM,filePath);
	}

	public void makeToast(string message){
		AndroidHelper.getInternalAndroidObject ().Call (_methodMakeToast, message);
	}

	public bool checkNetworkAvailable(){
		return AndroidHelper.getInternalAndroidObject ().Call<bool> (_methodCheckNetworkAvailable);
	}


	public bool isTablet(){
		return AndroidHelper.getInternalAndroidObject ().Call<bool> (_methodIsTablet);
	}

	public float densityScale(){
		return  AndroidHelper.getInternalAndroidObject ().Call<float> (_methodDensityScale);
	}

	public int listAssetFiles(string path){
		return AndroidHelper.getInternalAndroidObject().Call<int>(_methodListAssetFiles,path);
	}

	public void endSession(){
		AndroidHelper.getInternalAndroidObject().Call(_methodEndSession);
	}

	public void popAlertDialog(string message){
		AndroidHelper.getInternalAndroidObject().Call(_methodPopAlertDialog,message);
	}

	public void showNetworkAlert(){
		AndroidHelper.getInternalAndroidObject().Call(_methodShowNetworkAlertDlg);
	}

	public void showLoadingView(){
		AndroidHelper.getInternalAndroidObject().Call(_methodShowLoadingView);
	}

	public void removeLoadingView(){
		AndroidHelper.getInternalAndroidObject().Call(_methodCloseLoadingView);
	}

	public Texture2D getTexture2D()
	{
		int width = Screen.width;
		int height = Screen.height;
		Texture2D tex = new Texture2D(width,height,TextureFormat.RGB24,false);
		tex.ReadPixels(new Rect(0,0,width,height),0,0,false);
		tex.Compress(false);//对屏幕缓存进行压缩
		tex.Apply();
		return tex;
	}
		
}


